<!DOCTYPE html>
<html>
    <head>

        <title>Insert title here</title>
        <script>

            /**
             * First, we create a function that stores the name of the visitor in a cookie variable:
             * */
            function setCookie(cname, cvalue, exdays)
            {
                var d = new Date();
                d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
                var expires = "expires=" + d.toGMTString();
                document.cookie = cname + "=" + cvalue + "; " + expires;
            }

            /**
             * A Function to Get a Cookie
             * Then, we create a function that returns the value of a specified cookie:
             * */
            function getCookie(cname) {

                var name = cname + "=";
                var ca = document.cookie.split(';');
                for (var i = 0; i < ca.length; i++)
                {
                    var c = ca[i].trim();
                    if (c.indexOf(name) == 0)
                        return c.substring(name.length, c.length);
                }
                return "";
            }


            function checkCookie() {
                var username = getCookie("username");

                if (username != "")
                {
                    alert("Welcome again " + username);
                }
                else
                {
                    username = prompt("Please enter your name:", "");
                    if (username != "" && username != null)
                    {
                        setCookie("username", username, 365);
                    }
                }

            }
        </script>
    </head>
    <body onload="checkCookie()">
        <h2>What are Cookies?</h2>
        <p>Cookies are data, stored in small text files, on your computer.</p>
        When a web server has sent a web page to a browser, the connection is shut down, and the server forgets everything about the user.
        Cookies were invented to solve the problem "how to remember information about the user":
        <ul>
            <li>When a user visits a web page, his name can be stored in a cookie.</li>
            <li>Next time the user visits the page, the cookie "remembers" his name.</li>
        </ul>
        When a browser request a web page from a server, cookies belonging to the page is added to the request. This way the server gets the necessary data to "remember" information about users.
        <p>Cookies are saved in name-value pairs like:</p>
        <div>username=John Doe</div>
        <p>When a browser request a web page from a server, cookies belonging to the page is added to the request. This way the server 
            gets the necessary data to "remember" information about users.</p>
        <h2>Create a Cookie with JavaScript</h2>
        JavaScript can create cookies, read cookies, and delete cookies with the property <b>document.cookie.</b>
        <hr>
        With JavaScript, a cookie can be created like this:<br>
        <i>document.cookie="username=John Doe";</i>
        <hr>
        You can also add an expiry date (in UTC or GMT time). 
        By default, the cookie is deleted when the browser is closed:<br>
        <i>document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 GMT";</i>
        <hr>
        With a path parameter, you can tell the browser what path the cookie belongs to. By default, the cookie belongs to the current page.<br>
        <i>document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 GMT; path=/";</i>
        <hr>
        <h2>Read a Cookie with JavaScript</h2>
        With JavaScript, cookies can be read like this:<br>
        <i>var x = document.cookie;</i>
        <hr>
        <b>Note:</b>document.cookie will return all cookies in one string much like: cookie1=value; cookie2=value; cookie3=value;
        <hr>
        <h2>Change a Cookie with JavaScript</h2>
        With JavaScript, you can change a cookie the same way as you create it:<br>
        <i>document.cookie="username=John Smith; expires=Thu, 18 Dec 2013 12:00:00 GMT; path=/";</i>
        The old cookie is overwritten.
        <hr>
        <h2>Delete a Cookie with JavaScript</h2>
        Deleting a cookie is very simple. Just set the expires parameter to a passed date:<br>
        <i>document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 GMT";</i><br>
        Note that you don't have to specify a cookie value when you delete a cookie.
        <hr>
        <h2>JavaScript Cookie Example</h2>




    </body>
</html>